<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList2" @findClicked="findClicked2" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">指令资料</h1>
        <Table
          :tableHeader="tableHeader"
          :tableData="tableData"
          selection
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
          :summaryData="summaryData"
          summary
          @selectList="selectTableList"
        />
        <el-card shadow="never" :body-style="{ padding: '5px' }">
          <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
        </el-card>
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList3" @findClicked="findClicked3" />
        <h1 class="f16">对账明细汇总</h1>
        <Table
          :tableHeader="tableHeader2"
          :tableData="tableData2"
          selection
          class="mt10"
          :isLoading="isLoading2"
          @currentRow="currentRow2"
          :TableHeight="40"
          :summaryData="summaryData2"
          summary
          @selectList="selectTableList2"
        />
        <h1 class="f16">收款记录</h1>
        <Table
          :tableHeader="tableHeader3"
          :tableData="tableData3"
          :isLoading="isLoading2"
          @currentRow="currentRow2"
          :TableHeight="40"
          :summaryData="summaryData3"
          summary
        />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ExternalReceivables'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import type { formListType } from '@/components/Find/index.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

const formList = ref<any>([
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '型体',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '客户',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '下单日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '指令交期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '结算月份',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '结算日期',
    dateValue: ''
  },
  {
    type: 'checkbox',
    checkboxLabel: '生产未完成',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '生产已完成',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '未对账',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '已对账',
    checked: false
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])
const formList2 = ref([
  {
    type: 'input',
    inputPlaceholder: '结算月份',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '结算方式',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '结算'
  },
  {
    type: 'input',
    inputPlaceholder: '对账数量',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '对账单价',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '备注信息',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '对账修正'
  },
  {
    type: 'button',
    checkboxLabel: '备注确认'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])
const formList3 = ref<any>([
  {
    type: 'input',
    inputPlaceholder: '单据号码',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '客户',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '结算月份',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '结算日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '取消结算'
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录检核'
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录取消检核'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])

// 表一模块
// 表一按钮
const findClicked = (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const findClicked2 = (content: string) => {
  if (content === '置空') {
    formList2.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 5,
    num: 0
  },
  {
    index: 13,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '有欠数')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '无欠数')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return 'N'
  } else if (Audited.checked) {
    return 'Y'
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/pcFactorQuery/queryMtlHead', {
    brand: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    color: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入颜色'
    )?.inputValue,
    deliveryEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
    )?.dateValue?.[1],
    deliveryStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
    )?.dateValue?.[0],
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    shortStatus: cfmFlag() //	有无欠数(Y/N)	string	body	true
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  currentRow()
  summaryData.value[0].num = tableDataList.sum[0].orderCount
  summaryData.value[1].num = tableDataList.sum[0].surplusCount
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const tableHeader = ref([
  {
    label: '客户',
    prop: 'slipNo'
  },
  {
    label: '指令',
    prop: 'slipDate'
  },
  {
    label: '型体',
    prop: 'dutyDeptName'
  },
  {
    label: '颜色',
    prop: 'dutyDeptName'
  },
  {
    label: '订单数',
    prop: 'sysUser'
  },
  {
    label: '下单日期',
    prop: 'dutyMemo'
  },
  {
    label: '交期',
    prop: 'dutyMemo'
  },
  {
    label: '制程',
    prop: 'dutyMemo'
  },
  {
    label: '冲裁',
    prop: 'dutyMemo'
  },
  {
    label: '配套',
    prop: 'dutyMemo'
  },
  {
    label: '针车',
    prop: 'dutyMemo'
  },
  {
    label: '成型',
    prop: 'dutyMemo'
  },
  {
    label: '外接单价',
    prop: 'dutyMemo'
  },
  {
    label: '收款客户',
    prop: 'dutyMemo'
  },
  {
    label: '结算月份',
    prop: 'dutyMemo'
  },
  {
    label: '对账数量',
    prop: 'dutyMemo'
  },
  {
    label: '对账单价',
    prop: 'dutyMemo'
  },
  {
    label: '金额',
    prop: 'dutyMemo'
  },
  {
    label: '备注',
    prop: 'dutyMemo'
  }
])
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
  // 给表二传指令号
  // let table2Data = await service.get<any, any>(`/pcFactorQuery/queryMtlAttribute/${lotNo}`)
  // tableData2.value = table2Data.slip
}
const selectedLists = ref([])
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}

const tableHeader2 = ref([
  {
    label: '单据号码',
    prop: 'slipNo'
  },
  {
    label: '结算日期',
    prop: 'slipDate'
  },
  {
    label: '收款客户',
    prop: 'dutyDeptName'
  },
  {
    label: '结算月份',
    prop: 'sysUser'
  },
  {
    label: '结算方式',
    prop: 'dutyMemo'
  },
  {
    label: '金额',
    prop: 'dutyMemo'
  }
])
const selectedLists2 = ref([])
const selectTableList2 = (selectedList: any) => {
  selectedLists2.value = selectedList
}
// 表二模块
// 表二按钮
const findClicked3 = (content: string) => {
  if (content === '置空') {
    formList3.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData2 = ref([])
const isLoading2 = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData2 = ref<summaryDataType[]>([
  {
    index: 6,
    num: 0
  }
])
// 分页器
const currentPage2 = ref(1)
const pageSize2 = ref(40)
const total2 = ref(0)
const handleSizeChange2 = (val: number) => {
  pageSize2.value = val
  getTableList2()
}
const handleCurrentChange2 = (val: number) => {
  currentPage2.value = val
  getTableList2()
}
// 获取表数据
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.post<any, any>('/pcFactorQuery/queryMtlHead', {
    brand: formList3.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    color: formList3.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入颜色'
    )?.inputValue,
    deliveryEndDate: formList3.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
    )?.dateValue?.[1],
    deliveryStartDate: formList3.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
    )?.dateValue?.[0],
    lotNo: formList3.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading2.value = false

  tableData2.value = tableDataList.list
  total2.value = tableDataList.total
  currentRow2()
  summaryData2.value[0].num = tableDataList.sum[0].orderCount
}, 500)
onMounted(() => {
  getTableList2()
})
watch(formList2.value, () => {
  getTableList2()
})
// 点击表给另一张表传数据
const currentTableRow2 = ref({}) as any
const currentRow2 = async (row: any = tableData.value[0]) => {
  currentTableRow2.value = row
  getTableList3()
  // 给表二传指令号
  // let table2Data = await service.get<any, any>(`/pcFactorQuery/queryMtlAttribute/${lotNo}`)
  // tableData2.value = table2Data.slip
}

// 表三模块
const tableData3 = ref([])
const isLoading3 = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData3 = ref<summaryDataType[]>([
  {
    index: 3,
    num: 0
  }
])
// 分页器
const currentPage3 = ref(1)
const pageSize3 = ref(40)
const total3 = ref(0)
const handleSizeChange3 = (val: number) => {
  pageSize3.value = val
  getTableList3()
}
const handleCurrentChange3 = (val: number) => {
  currentPage3.value = val
  getTableList3()
}
// 获取表数据
const getTableList3 = debounce(async () => {
  isLoading3.value = true
  let tableDataList = await service.post<any, any>('/pcFactorQuery/queryMtlHead', {
    brand: formList3.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    color: formList3.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入颜色'
    )?.inputValue,
    deliveryEndDate: formList3.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
    )?.dateValue?.[1],
    deliveryStartDate: formList3.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
    )?.dateValue?.[0],
    lotNo: formList3.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    pageNum: currentPage3.value,
    pageSize: pageSize3.value
  })
  isLoading3.value = false
  tableData3.value = tableDataList.list
  total3.value = tableDataList.total
  // currentRow3()
  summaryData3.value[0].num = tableDataList.sum[0].orderCount
}, 500)
const tableHeader3 = ref([
  {
    label: '收款客户',
    prop: 'slipNo'
  },
  {
    label: '实收日期',
    prop: 'slipDate'
  },
  {
    label: '金额',
    prop: 'dutyDeptName'
  },
  {
    label: '备注',
    prop: 'sysUser'
  },
  {
    label: '操作员',
    prop: 'dutyMemo'
  },
  {
    label: '未付款金额',
    prop: 'dutyMemo'
  }
])
</script>

<style lang="less" scoped></style>
